package ypmits.utils {
	import flash.display.DisplayObject;
	import flash.geom.ColorTransform;

	/**
	 * @author Dave Lenz
	 */
	public class AnimUtil {

		private static const ONE_PERCENT : Number = .0111111111;

		public static function rotationHandler( tier : DisplayObject, propName : String = "rotationY" ) : void 
		{
			var c : Number;
			var r : Number = Math.abs( tier[propName] );
			
			r >= 360 ? r = 0 : null;
			
			if( ( r >= 0 ) && ( r <= 90 ) ) c = 1 - Math.abs( ONE_PERCENT * r );
			if( ( r > 90 ) && ( r <= 180 ) ) c = 1 - Math.abs( ( 180 - r ) * ONE_PERCENT );
			if( ( r > 180 ) && ( r <= 270 ) ) c = Math.abs( ( r - 270 ) * ONE_PERCENT );
			if ( ( r > 270 ) && ( r <= 360 ) ) c = 1 - Math.abs( ( r - 360 ) * ONE_PERCENT );
			
			tier.transform.colorTransform = new ColorTransform( c, c, c, 1, 0, 0, 0, 0 );
		}

		public static function rotationHandlerImageHolder( tier : DisplayObject, propName : String = "rotationY" ) : void 
		{
			var c : Number;
			var r : Number = Math.abs( tier[propName] );
			
			r >= 360 ? r = 0 : null;
			
			if( ( r >= 0 ) && ( r <= 90 ) ) c = 1 - Math.abs( ONE_PERCENT * r );
			if( ( r > 90 ) && ( r <= 180 ) ) c = 1 - Math.abs( ( 180 - r ) * ONE_PERCENT );
			if( ( r > 180 ) && ( r <= 270 ) ) c = Math.abs( ( r - 270 ) * ONE_PERCENT );
			if ( ( r > 270 ) && ( r <= 360 ) ) c = 1 - Math.abs( ( r - 360 ) * ONE_PERCENT );

			if( tier[propName] >= 90 ) {
				tier.visible = false;
			} else {
				tier.visible = true;
				tier.transform.colorTransform = new ColorTransform( c, c, c, 1, 0, 0, 0, 0 );
			}
			
			tier.transform.colorTransform = new ColorTransform( c, c, c, 1, 0, 0, 0, 0 );
		}
		
		
		public static function rotationHandlerDouble( tier1 : DisplayObject, tier2 : DisplayObject, propName : String = "rotationY" ) : void 
		{
			var c : Number;
			var r : Number = Math.abs( tier1[propName] );
			
			r >= 360 ? r = 0 : null;
			
			if( ( r >= 0 ) && ( r <= 90 ) ) c = 1 - Math.abs( ONE_PERCENT * r );
			if( ( r > 90 ) && ( r <= 180 ) ) c = 1 - Math.abs( ( 180 - r ) * ONE_PERCENT );
			if( ( r > 180 ) && ( r <= 270 ) ) c = Math.abs( ( r - 270 ) * ONE_PERCENT );
			if( ( r > 270 ) && ( r <= 360 ) ) c = 1 - Math.abs( ( r - 360 ) * ONE_PERCENT );
			
			if( tier1[propName] >= 90 ) {
				tier1.visible = false;
			} else {
				tier1.visible = true;
				tier1.transform.colorTransform = new ColorTransform( c, c, c, 1, 0, 0, 0, 0 );
			}
			if( tier2[propName] >= 270 ) {
				tier2.visible = true;
				tier2.transform.colorTransform = new ColorTransform( c, c, c, 1, 0, 0, 0, 0 );
			} else {
				tier2.visible = false;
			}
		}
	}
}
